package xyz.thoughtset.viewer.common.api.advice;


import org.apache.catalina.core.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import xyz.thoughtset.viewer.common.api.result.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import xyz.thoughtset.viewer.common.exc.event.ExcMsgEvent;
import xyz.thoughtset.viewer.common.exc.exceptions.ExecException;
import xyz.thoughtset.viewer.common.exc.service.ExcInfoService;

@Slf4j
//@RestControllerAdvice(basePackageClasses = {
//        BaseController.class
//})
//@RestControllerAdvice(annotations={RestController.class})
@RestControllerAdvice
public class ExceptionAdvice {
    @Autowired
    private ObjectMapper mapper;
    @Autowired
    private ExcInfoService excInfoService;
    @Autowired
    private ApplicationEventPublisher eventPublisher;
    
    @ExceptionHandler({ExecException.class})
    public Result<?> handle(ExecException ex) {
        log.error(ex.toString(), ex);
        eventPublisher.publishEvent(new ExcMsgEvent(this,ex));
        return Result.failed(ex.getMessage());
    }

    @ExceptionHandler({Exception.class})
    public Result<?> handle(Exception ex) {
        log.error(ex.toString(), ex);
        return Result.failed(ex.getMessage());
    }

}